package ying.cookie03;
/**
 * ClassName: SetCookieMaxAge
 * Package: ying.cookie03
 * Description:
 *
 * @Author 刘英杰
 * @Create 2024/10/5 9:39
 * @Version 1.0
 */

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * 创建一个cookie,设置cookie的生命周期(最大存活时间)
 *   public void setMaxAge(int expiry): 设置cookie的生命周期(最大存活时间)
 */

@WebServlet("/setCookieMaxAge")
public class SetCookieMaxAge extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie = new Cookie("id", "789");
        cookie.setPath(request.getContextPath());
        cookie.setMaxAge(60); //60秒
        response.addCookie(cookie);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
